CalcProbCompIsReplaced <- function(K) { #,PartTree,ParentIdx,ChildIdx,PartProbs,Ph) {
  
  # Now that we have the probability of any given component being part of
  # the K chosen, we use that probability to weigh the sub-component's
  # in its branch.  I want the probability that a component will have to be replaced
  # for a specific k, and up to K.
  #
  # Inputs
  #   Ph - scalar btwn 0-1 - Probability of the events that lead to the 
  #        exploration of this branch. NECESSARY? TEST
  #
  # Outputs
  #   Pb - Bx1 vector of each permutation's probability, or average probability
  #
  # Written by: Sean M Gonzalez, Willcor Inc., October 2012 
  #_____________________________________________________________________
  
  source("FormatRawTree.R")
  source("ID_PartsParents.R")
  source("ID_PartsChildren.R")
  source("BranchedNcK.R")
  source("ExtractKBranchCombos.R")
#   source('~/Documents/Sean/Willcor/Aegis SM3/Sustainability/Risk Model/WillcorDSI-PPODS/StarsAndBars.R')
  source("NchooseK_FlatBranchHybrid.R")
  
  if (file.exists("ProbCompIsReplaced.data")) {
    rm("ProbCompIsReplaced.data") }
  pcird <- file("ProbCompIsReplaced.data", "w")
  
  Part <- FormatRawTree()
#   K = 9
  Out <- ID_PartsParents(Part$Tree)
  ParentIdx <- Out$ParentIdx
  
  cat("Entering ID_PartsChildren.R\n",file=pcird)
  ChildIdx  <- ID_PartsChildren(Part$Tree)
  
  cat("Entering BranchedNcK.R \n",file=pcird)
  BranchProbs <- BranchedNcK(Part$Tree,(1-Part$Probs),Part$Replace,ParentIdx,ChildIdx,K)
  BranchProbs$Part <- Part
  
  close(pcird)
  # Now that I have the probabilities up each branch, 
  # I vaguely remember something about having to go across branches???
  
  return(BranchProbs)
  
# OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD 
#   TotProbReplaced <- matrix(integer(N*K),N,K)
#   probCompReplaced   <- TotProbReplaced
#   probNotReplaced    <- TotProbReplaced
#   eachK_ProbReplaced <- TotProbReplaced
#   compList = 1:N
#   L = max(PartTree)
#   
#   for (c in compList) {
#     cl = PartTree[c] # This component's level
#     # find the number of components at a higher level
#     P <- (cl-1) #numParents #sum(PartTree < l)
#     compParentIdx <- ParentIdx[[c]]
#     
#     # We need to know the probability of replacing a component
#     # based on the parents, the component's children, and the remaining branches.
#     elseIdx = !logical(N)
#     elseIdx[c] = FALSE
#     avgPe = mean(PartProbs[elseIdx])
#     TotProbReplaced[c,1] = (1-PartProbs[c])*avgPe^(N-1)
#     
#     for (k in 2:K) {
#       for (l in 1:L) {
# 
#         # Now that I have the probability for k, I need the probability through K
#         TotProbReplaced[c,k] = eachK_ProbReplaced[c,k] + (1-eachK_ProbReplaced[c,k])*TotProbReplaced[c,k-1]
#       }
#     }
#   }
#   Output$probCompReplaced   = probCompReplaced
#   Output$probNotReplaced    = probNotReplaced
#   Output$eachK_ProbReplaced = eachK_ProbReplaced
#   Output$TotProbReplaced    = TotProbReplaced
  
  #=========================================================================================
  # For plotting, let's organize by the number of parents
  sortedParents = sort(PartTree-1,index.return=TRUE)
  xx = sortedParents$ix
  #   plot(TotProbReplaced[9,],type="l",log="y")
  partProbRatio = TotProbReplaced[xx,9]/(1-PartProbs[xx])
  plot(compList,partProbRatio,type="l",log="y",col="black",xlab="",ylab="Failure Probability Ratio") #,yaxt='n') #,ylim=c(0.5,6))
  par(new=TRUE)
  plot(compList,sortedParents$x,type="l",col="red",xlab="Sorted Component #",ylab="",yaxt='n') #ylim=c(0.5,6))
  #   axis(2, at=1:5) #pretty(partProbRatio,5) )
  axis(4, at=1:6,col="red",col.axis="red") #pretty(sortedParents$x,5) )
  title(main="Hierarchy vs LSP \nComponent Priority Changes")
  mtext("Number of Parents",side=4,col="red",padj=-3)
  
  plot(compList,TotProbReplaced[xx,9],type="l",log="y",col="black",xlab="",ylab="Probability")
  par(new=TRUE)
  plot(compList,sortedParents$x,type="l",col="red",xlab="Sorted Component #",ylab="",yaxt='n') #ylim=c(0.5,6))
  #   axis(2, at=1:5) #pretty(partProbRatio,5) )
  axis(4, at=1:6,col="red",col.axis="red") #pretty(sortedParents$x,5) )
  title(main="Component Replacement Probability")
  mtext("Number of Parents",side=4,col="red",padj=-3)
  
  #   plot(compList,TotProbReplaced[xx,9],ylab="",log="y",type="l")
  #   par(new=TRUE)
  #   lines(compList,(1-PartProbs[xx]),log="y",col="red")
  #   plot(branchIrrelevant[xx],log="y",type="l",col="red")
  
  #   library(rgl)
  #   rgl.surface(1:N,1:K,probReplace)
  #=========================================================================================
  
#   write.table(TotProbReplaced,"Branch_Based_Replace_Probabilities.csv",sep=",",col.names=FALSE)
  #   write.table(BranchSigRel,"BranchPriorityDensities.csv",sep=",",col.names=FALSE)
}